<?php

 /**
  * Grid Helper
  * 
  * @package   
  * @author tienhc <hatuhn@gmail.com>
  * @copyright 24h <@see http://24h.com.vn>
  * @version 2009
  * @access public
  */
  
class GridHelper extends HtmlHelper {
 	
 	/**
	 * @var array $__header;
	 *
	 * @access private;
	 */
	private $__header = null;
	
	/**
	 * @var array $__content;
	 *
	 * @access private;
	 */
	private $__content = null;
	
	/**
	 * @var array $__key;
	 * 
	 * @access private.
	 */
	private $__keys = array();
	
	/**
     * set header data
     * 
     * @access public
     * @return GridHelper Object;
	 */
	public function setHeader($header){
		$this->__header = $header;
		return $this;
	}
	
	/**
     * get header data;
     * 
	 */
	public function getHeader(){
		return $this->__header;
	}
	
	/**
	 * get header of  grid control.
	 * 
	 * @param array $options.
	 * @access protected
	 * @return string
	 */
	protected function _getGridHeader($options){
		if(!isset($this->__header)) return;
		$tring = '<tr>';
		if(isset($options['key'])){
			$string .= '<th class="action"><input type="checkbox" name="check_all" id="cbxCheckAll"></th>';
		}
		foreach($this->__header as $key => $text){
			$string .='<th>'.$text.'</th>';
			$this->__keys[] = $key;
		}
		$string .= '</tr>';
		
		return $string;
	}
	
	/**
	 * get body of  grid control.
	 * 
	 * @param array $gridData
	 * @param array $options.
	 * @access protected
	 * @return string 
	 */
	protected function _getGridBody($gridData, $options, $objectCallback=null){	
		$string = '<tbody>';	
		// if none display row following keys 
		if(empty($this->__keys)){
				
		} else {
			$i=0;
			foreach($gridData as $data){
				$string = '<tr '.((++$i%2==0)?$options['row-even']:$options['row-old']).'>';
				if(isset($options['key'])){
					$string .= '<td class="action-record"><input value="'.$data[$options['key']].'" type="checkbox" name="keys[]" id="key'.$key.'"></td>';
				}
				foreach($data as $key=>$text){
	 				// support pre display by calling method onDisplay which was build-in object callback.	
					if($objectCallback !=null){
						if(method_exists($objectCallback,'onGridDisplay')){
							$string .= call_user_method_array('onGridDisplay', $objectCallback, array($key, $data[$key]));	
						}	
					} else {
						$string .= '<td >'.$text.'</td>';		
					}	
				}
				$string .= '</tr>';		
			}		
		}	
		$string .= '</tbody>';	
		
		return $string;
	}
	
	/**
	 * paginate
	 * 
	 * @param integer $total
	 * @param integer $page
	 * @access protected
	 * @return 
	 */
	protected function _paginate($total, $page){
		
	}
	
	/**
     * display grid control
     * 
     * @param array $options contain custom option.
     * @param Object $objectCallback is a object which call to proccess data in runtime perior.
     * @access public.
     * @return none.
	 */
	public function display($data, $options=array(), $objectCallback=null){
		// default options.
		$default = array('row-old'  => 'old',  // class flag for row - old
						 'row-even' => 'even', // class flag for row - even
						 'total'    => '0',    // total record 
						 'page'     => 1,      // current page. 
						 'max'      => 30,     // max number record display in page.
	 					 'sortable' => true    // set true|false to endable|disable using sortable. 
	 	);  
	 	// override options 
		$options = array_merge($default, $options);	
		// buid grid control.
		$string = '<table width="100%" border="0" cellpadding="0" cellspacing="0">';
		$string .= $this->_getGridHeader($options);	
		$string .= $this->_getGridBody($data, $options, $objectCallback);
		$string .='</table>';
		// if enable paginate control
		if($options['sortable']){
  			$string .= '<div class="grid-paginate">'.$this->_paginate($options['max'], $options['page']).'</div>';
		}
		print $string; 
		return;
	}
}
 
